﻿function FeedsStorage()
{
    this.ItemsFromQzone = new Array();
    this.ItemsFromWeibo = new Array();
    this.time = 0;
}
FeedsStorage.prototype.AddItem = function(item)
{
    if(item == null)return;
    if(item.from == 0)this.ItemsFromQzone.push(item);
    else if(item.from == 1)this.ItemsFromWeibo.push(item);
}
FeedsStorage.prototype.Save = function(uin)
{
    this.time = Date.parse(new Date());
    localStorage.setItem(uin, this.ToJson());
}
FeedsStorage.prototype.Load = function(uin)
{
    try
    {
        var storage = localStorage.getItem(uin);
        if(!storage)return;
        var json=JSON.parse(storage);
        if(json.ItemsFromQzone != undefined)this.ItemsFromQzone = json.ItemsFromQzone;
        if(json.ItemsFromWeibo != undefined)this.ItemsFromWeibo = json.ItemsFromWeibo;
    }
    catch(e)
    {
    }
    return;
}
FeedsStorage.prototype.ToJson = function()
{
    return JSON.stringify(this);   
}

function FeedsItem()
{
    this.from = 0;  //0空间，1微博
    this.type = 0;  //0说说，1日志
    this.title = "";    //标题，仅日志
    this.content = "";  //内容
    this.picture = "";  //图片链接
    this.fromurl = "";  //来源链接，仅微博
    this.contenturl = ""; //正文链接
    this.abstime = 0;  //绝对时间，由一个整数表示的时间单位为秒
    this.id = "";   //微博id
}

function FeedsStorageAutoDelete()
{
    this.SortArray = new Array();
    this.SortItem = function()
    {
        var item = new Object(); 
        item.time = null;
        item.key = "";
        return item;
    }
    this.InsertSortArray = function(sort_Array,x)  //对所有用户feeds按时间排序
    {
        var i = 0;
        for(i=0;i<sort_Array.length;i++)
        {
            if(sort_Array[i].time >= x.time)
            {
                for(var j=sort_Array.length; j>i; j--)
                {
                    sort_Array[j]=sort_Array[j-1];      
                }
                break;
            }
        }
        sort_Array[i]=x;
    }
    this.CurIndex = 0;
    this.CurDeleteIndex = 0;
    this.AutoDelete = function(maxCnt, deleteCnt)
    {
        if(this.CurIndex < localStorage.length)
        {
            for(var cnt = 0; cnt < 50 && this.CurIndex < localStorage.length; ++this.CurIndex, ++cnt)
            {
                var sortItem = new this.SortItem();
                sortItem.key = localStorage.key(this.CurIndex);
                var storage = new FeedsStorage();
                storage.Load(sortItem.key);
                sortItem.time = storage.time;
                this.InsertSortArray(this.SortArray, sortItem);
            }
            var self = this;
            setTimeout(function(){self.AutoDelete(maxCnt, deleteCnt);}, 20);
        }
        else if(this.CurDeleteIndex < deleteCnt && this.CurDeleteIndex < this.SortArray.length)
        {
            if(deleteCnt > this.SortArray.length)deleteCnt = this.SortArray.length;
            for(var cnt = 0; cnt < 50 &&  this.CurDeleteIndex < deleteCnt; ++this.CurDeleteIndex, ++cnt)
            {
                localStorage.removeItem(this.SortArray[this.CurDeleteIndex].key);
            }
            var self = this;
            setTimeout(function(){self.AutoDelete(maxCnt, deleteCnt);}, 20);
        }
        else
        {
            if(this.DeleteEnd != null)this.DeleteEnd();
        }
    }
    this.DeleteEnd = null;
}

